import { createSlice } from "@reduxjs/toolkit";
import { CollectionFolder } from "@/apis/shared.ts";

// 定义初始状态类型
interface UserInfoState {
  // token: string;
  email: string;
  password: string;
  isRemember: boolean;
  userLikesList: string[];
  userCollectionList: CollectionFolder[];
  authorAvatar: string;
  shortToken: string;
  longToken: string;
}

const userInfoStore = createSlice({
  name: "userInfoStore",
  initialState: {
    // 短 token 值
    shortToken: localStorage.getItem("shortToken") || "",
    // 长 token 值
    longToken: localStorage.getItem("longToken") || "",
    // token值
    // token: localStorage.getItem("token") || "",
    // 用户email值
    email: localStorage.getItem("email") || "",
    // 用户密码
    password: localStorage.getItem("password") || "",
    // 是否记住密码
    isRemember:
      JSON.parse(localStorage.getItem("isRemember") || "false") || false,
    // 用户的收藏夹列表
    userCollectionList:
      JSON.parse(localStorage.getItem("userCollectionList") || "[]") || [],
    // 用户的点赞列表
    userLikesList:
      JSON.parse(localStorage.getItem("userLikesList") || "[]") || [],
    // 作者头像
    authorAvatar: localStorage.getItem("authorAvatar") || "",
  } as UserInfoState,
  reducers: {
    // 设置短 token
    setShortToken: (state, actions: { payload: string }) => {
      state.shortToken = actions.payload;
      localStorage.setItem("shortToken", state.shortToken);
    },

    // 设置长 token
    setLongToken: (state, actions: { payload: string }) => {
      state.longToken = actions.payload;
      localStorage.setItem("longToken", state.longToken);
    },

    // 清除短 token
    clearShortToken: (state) => {
      state.shortToken = "";
      localStorage.removeItem("shortToken");
    },

    // 清除长 token
    clearLongToken: (state) => {
      state.longToken = "";
      localStorage.removeItem("longToken");
    },

    // // 设置token
    // setToken: (state, actions: { payload: string }) => {
    //   state.token = actions.payload;
    //   localStorage.setItem("token", state.token);
    // },

    // // 清除token
    // clearToken: (state) => {
    //   state.token = "";
    //   localStorage.removeItem("token");
    // },

    // 设置email
    setEmailToStore: (state, actions: { payload: string }) => {
      state.email = actions.payload;
      localStorage.setItem("email", state.email);
    },

    // 清除email
    clearEmail: (state) => {
      state.email = "";
      localStorage.removeItem("email");
    },

    // 设置password
    setPasswordToStore: (state, actions: { payload: string }) => {
      state.password = actions.payload;
      localStorage.setItem("password", state.password);
    },

    // 清除password
    clearPassword: (state) => {
      state.password = "";
      localStorage.removeItem("password");
    },

    // 设置是否记住密码
    setIsRememberToStore: (state, actions: { payload: boolean }) => {
      state.isRemember = actions.payload;
      localStorage.setItem("isRemember", JSON.stringify(state.isRemember));
    },

    // 清除是否记住密码
    clearIsRemember: (state) => {
      state.isRemember = false;
      localStorage.removeItem("isRemember");
    },

    // 设置用户的点赞列表
    setUserLikesListToStore: (state, actions: { payload: string[] }) => {
      state.userLikesList = actions.payload;
      localStorage.setItem(
        "userLikesList",
        JSON.stringify(state.userLikesList)
      );
    },

    // 清除用户的点赞列表
    clearUserLikesList: (state) => {
      state.userLikesList = [];
      localStorage.removeItem("userLikesList");
    },

    // 添加用户收藏夹列表
    setUserCollectionList: (
      state,
      actions: { payload: CollectionFolder[] }
    ) => {
      state.userCollectionList = actions.payload;
      localStorage.setItem(
        "userCollectionList",
        JSON.stringify(state.userCollectionList)
      );
    },

    // 添加用户收藏夹
    addUserCollection: (
      state,
      actions: { payload: { folderName: string; folderId: string } }
    ) => {
      const { folderName, folderId } = actions.payload;

      state.userCollectionList.push({
        folderName,
        collectItem: [],
        _id: folderId,
      });
    },

    // 删除用户收藏夹
    deleteUserCollection: (
      state,
      actions: { payload: { folderName: string } }
    ) => {
      const { folderName } = actions.payload;

      // 重新赋值一下这个userCollectionList
      state.userCollectionList = state.userCollectionList.filter(
        (item) => item.folderName !== folderName
      );

      // 重新存储到localStorage
      localStorage.setItem(
        "userCollectionList",
        JSON.stringify(state.userCollectionList)
      );
    },

    // 清除用户收藏夹
    clearUserCollection: (state) => {
      // 赋值空数组
      state.userCollectionList = [];
      // 从localStorage中删除
      localStorage.removeItem("userCollectionList");
    },

    // 添加用户收藏夹列表文章
    addUserCollectionArticle: (
      state,
      actions: {
        payload: {
          folderName: string; // 收藏夹名称
          _id: string; // 文章id
          title: string; // 文章标题
          summary: string; // 文章摘要
        };
      }
    ) => {
      const { folderName, _id, title, summary } = actions.payload;

      // 找到对应的收藏夹
      state.userCollectionList.forEach((item) => {
        if (item.folderName === folderName) {
          // 文件夹存在
          item.collectItem.push({
            title: title,
            _id: _id,
            summary: summary,
          });
        }
      });

      // 重新存储到localStorage
      localStorage.setItem(
        "userCollectionList",
        JSON.stringify(state.userCollectionList)
      );
    },

    // 删除用户收藏夹列表文章
    deleteUserCollectionArticle: (
      state,
      actions: {
        payload: {
          folderName: string; // 收藏夹名称
          _id: string; // 文章id
        };
      }
    ) => {
      const { folderName, _id } = actions.payload;

      // 找到对应的收藏夹
      state.userCollectionList.forEach((item) => {
        if (item.folderName === folderName) {
          // 找到对应的文章，过滤掉对应的文章
          item.collectItem = item.collectItem.filter(
            (item) => item._id !== _id
          );
        }
      });

      // 重新存储到localStorage
      localStorage.setItem(
        "userCollectionList",
        JSON.stringify(state.userCollectionList)
      );
    },

    // 设置用户头像
    setUserAvatar: (state, actions: { payload: string }) => {
      state.authorAvatar = actions.payload;
      localStorage.setItem("authorAvatar", JSON.stringify(state.authorAvatar));
    },

    // 清除用户头像
    clearUserAvatar: (state) => {
      state.authorAvatar = "";
      localStorage.removeItem("authorAvatar");
    },

    // 用户退出登录
    userLoginout: (state) => {
      state.shortToken = ""; // 清除短 token
      localStorage.removeItem("shortToken");
      state.longToken = ""; // 清除长 token
      localStorage.removeItem("longToken");
      // state.token = ""; // 清除token
      // localStorage.removeItem("token");
      state.email = ""; // 清除email
      localStorage.removeItem("email");
      state.password = ""; // 清除password
      localStorage.removeItem("password");
      state.isRemember = false; // 清除是否记住密码
      localStorage.removeItem("isRemember");
      state.userLikesList = []; // 清除用户的点赞列表
      localStorage.removeItem("userLikesList");
      state.authorAvatar = ""; // 清除用户头像
      localStorage.removeItem("authorAvatar");
      state.userCollectionList = []; // 清除用户收藏夹列表
      localStorage.removeItem("userCollectionList");
    },
  },
});

// 获得actions
const {
  setShortToken,
  setLongToken,
  clearShortToken,
  clearLongToken,
  // setToken,
  // clearToken,
  setEmailToStore,
  clearEmail,
  setPasswordToStore,
  clearPassword,
  setIsRememberToStore,
  clearIsRemember,
  setUserLikesListToStore,
  clearUserLikesList,
  userLoginout,
  addUserCollection,
  deleteUserCollection,
  clearUserCollection,
  addUserCollectionArticle,
  deleteUserCollectionArticle,
  setUserCollectionList,
  setUserAvatar,
  clearUserAvatar,
} = userInfoStore.actions;

// 获得reducer
const userInfoReducer = userInfoStore.reducer;

export {
  setShortToken, // 设置短token
  setLongToken, // 设置长token
  clearShortToken, // 清除短token
  clearLongToken, // 清除token
  // setToken, // 设置token
  // clearToken, // 清除token
  setEmailToStore, // 设置email
  clearEmail, // 清除email
  setPasswordToStore, // 设置password
  clearPassword, // 清除password
  setIsRememberToStore, // 设置是否记住密码
  clearIsRemember, // 清除是否记住密码
  setUserLikesListToStore, // 设置用户的点赞列表
  clearUserLikesList, // 清除用户的点赞列表
  userLoginout, // 用户退出登录
  addUserCollection, // 添加用户收藏夹
  deleteUserCollection, // 删除用户收藏夹
  clearUserCollection, // 清除用户收藏夹
  addUserCollectionArticle, // 添加用户收藏夹列表文章
  deleteUserCollectionArticle, // 删除用户收藏夹列表文章
  setUserCollectionList, // 设置用户收藏夹列表
  setUserAvatar, // 设置用户头像
  clearUserAvatar, // 清除用户头像
};
export default userInfoReducer;
